#include<iostream>
#include<string>
#include<cerrno>
#include<cstring>

#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>


const std::string pathname = "/home/Dream/linux-warehouse/shm";
const int proj_id = 0x11223344;
const int size = 4096;
const std::string fifoname = "fifo";
key_t GetKey()
{
    key_t ret = ftok(pathname.c_str(), proj_id);
    if(ret < 0)
    {
        std::cerr << "errno: " << errno << ", errstring: " << strerror(errno) << std::endl;
        exit(1);
    }
    return ret;
}

int CreateShmHelper(key_t key,int flag)
{
    int shmid = shmget(key,size,flag);
    if(shmid < 0)
    {
        std::cerr << "errno: " << errno << ", errstring: " << strerror(errno) << std::endl;
        exit(1);
    }
    return shmid;
}
int CreateShm(key_t key) //创建共享内存
{
    return CreateShmHelper(key,IPC_CREAT | IPC_EXCL | 0666); //权限是八进制
}

int GetShm(key_t key) //获取共享内存
{
    return CreateShmHelper(key,IPC_CREAT);
}

bool MakeFifo()
{
    int ret = mkfifo(fifoname.c_str(),0666);
    if(ret < 0)
    {
        std::cerr << "errno: " << errno << ", errstring: " << strerror(errno) << std::endl;
        return false;
    }
    std::cout << "mkfifo success..." << std::endl;
    return true;
}
